﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Saxxon.LibDoomSharp.Lumps
{
    /// <summary>
    /// An entry in a THINGS Lump.
    /// </summary>
    public class ThingsEntry : SerializableBase
    {
        /// <summary>
        /// Length in bytes of an entry.
        /// </summary>
        public const int ENTRY_LENGTH = 10;

        /// <summary>
        /// X-location of the thing in units.
        /// </summary>
        public Int16 LocationX;

        /// <summary>
        /// Y-location of the thing in units.
        /// </summary>
        public Int16 LocationY;

        /// <summary>
        /// Angle in degrees of the thing.
        /// </summary>
        public ThingsLumpEntryAngle Angle;

        /// <summary>
        /// Thing type- this will vary among games.
        /// </summary>
        public UInt16 Type;

        /// <summary>
        /// Flags that determine how the thing is processed by the game.
        /// </summary>
        public ThingsLumpEntryFlags Flags;

        /// <summary>
        /// Read entry data.
        /// </summary>
        protected override void Read(Stream stream, int length)
        {
            DataReader reader = GetReader(stream);
            LocationX = reader.ReadInt16();
            LocationY = reader.ReadInt16();
            Angle = (ThingsLumpEntryAngle)reader.ReadUInt16();
            Type = reader.ReadUInt16();
            Flags = (ThingsLumpEntryFlags)reader.ReadUInt16();
        }

        /// <summary>
        /// Return string representation of this entry.
        /// </summary>
        public override string ToString()
        {
            return "Thing { Location=" + LocationX.ToString() + ", " +
                LocationY.ToString() + "; Angle=" +
                Angle.ToString() + "; Flags=" +
                Flags.ToString() + "; }";
        }

        /// <summary>
        /// Write entry data.
        /// </summary>
        protected override void Write(Stream stream)
        {
            DataWriter writer = GetWriter(stream);
            writer.WriteInt16(LocationX);
            writer.WriteInt16(LocationY);
            writer.WriteUInt16((UInt16)Angle);
            writer.WriteUInt16(Type);
            writer.WriteUInt16((UInt16)Flags);
        }
    }

    public enum ThingsLumpEntryAngle : ushort
    {
        North = 0,
        NorthEast = 45,
        East = 90,
        SouthEast = 135,
        South = 180,
        SouthWest = 225,
        West = 270,
        NorthWest = 315
    }

    [FlagsAttribute]
    public enum ThingsLumpEntryFlags : ushort
    {
        /// <summary>
        /// The Thing is present on difficulty levels 1 and 2.
        /// </summary>
        Skill1_2 = 0x0001,

        /// <summary>
        /// The Thing is present on difficulty level 3.
        /// </summary>
        Skill3 = 0x0002,

        /// <summary>
        /// The Thing is present on difficulty levels 4 and 5.
        /// </summary>
        Skill4_5 = 0x0004,

        /// <summary>
        /// The Thing does not respond to sound.
        /// </summary>
        Deaf = 0x0008,

        /// <summary>
        /// The Thing is present in multiplayer only.
        /// </summary>
        Multiplayer = 0x0010
    }
}
